package com.lai.thingsclient;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by lai on 2017/7/30.
 */

public class CarContrlAct extends AppCompatActivity implements View.OnTouchListener {
    private static final String TAG = "CarContrlAct";
    private boolean longClicked;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_contrl);
        initViewAndEvent();
    }

    private void initViewAndEvent() {
        findViewById(R.id.forward).setOnTouchListener(this);
        findViewById(R.id.back).setOnTouchListener(this);
        findViewById(R.id.left).setOnTouchListener(this);
        findViewById(R.id.right).setOnTouchListener(this);
    }



    private void cmd(String action) {
        String url = "http://barret.tunnel.qydev.com/carContrl?action="+action;
        OkHttpUtils
                .get()
                .url(url)
                .addHeader("User-agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int i) {
                        Log.e(TAG, "onError: "+e.getMessage() );
                    }

                    @Override
                    public void onResponse(String s, int i) {
                        Log.e(TAG, "onResponse: "+s );
                    }
                });
    }

    @Override
    public boolean onTouch(final View v, MotionEvent event) {
        Log.e("yj.chen", "onTouch");
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.e("yj.chen", "ACTION_DOWN");
            longClicked = true;
            Thread t = new Thread() {
                @Override
                public void run() {
                    super.run();
                    while (longClicked) {

                        switch (v.getId()) {
                            case R.id.forward:
                                cmd("forward");
                                break;
                            case R.id.back:
                                cmd("back");
                                break;
                            case R.id.left:
                                cmd("left");
                                break;
                            case R.id.right:
                                cmd("right");
                                break;

                        }
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e("yj.chen", "run end");
                    }
                }
            };
            t.start();

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            longClicked = false;
            Log.d("yj.chen", "ACTION_UP");
        }
        return true;

    }
}
